// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/testing/gtest_utils.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>
#include <pollux/functions/prestosql/types/bing_tile_type.h>
#include <pollux/functions/prestosql/types/hyper_log_log_type.h>
#include <pollux/functions/prestosql/types/json_type.h>
#include <pollux/functions/prestosql/types/tdigest_type.h>
#include <pollux/functions/prestosql/types/timestamp_with_time_zone_type.h>

namespace kumo::pollux::functions {
namespace {

class TypeOfTest : public functions::test::FunctionBaseTest {};

TEST_F(TypeOfTest, basic) {
  const auto typeOf = [&](const TypePtr& type) {
    auto data = make_row_vector({
        BaseVector::create_null_constant(type, 1, pool()),
    });
    return evaluateOnce<std::string>("typeof(c0)", data).value();
  };

  EXPECT_EQ("boolean", typeOf(BOOLEAN()));

  EXPECT_EQ("tinyint", typeOf(TINYINT()));
  EXPECT_EQ("smallint", typeOf(SMALLINT()));
  EXPECT_EQ("integer", typeOf(INTEGER()));
  EXPECT_EQ("bigint", typeOf(BIGINT()));

  EXPECT_EQ("real", typeOf(REAL()));
  EXPECT_EQ("double", typeOf(DOUBLE()));

  EXPECT_EQ("decimal(5,2)", typeOf(DECIMAL(5, 2)));
  EXPECT_EQ("decimal(25,7)", typeOf(DECIMAL(25, 7)));

  EXPECT_EQ("varchar", typeOf(VARCHAR()));
  EXPECT_EQ("varbinary", typeOf(VARBINARY()));

  EXPECT_EQ("timestamp", typeOf(TIMESTAMP()));
  EXPECT_EQ("timestamp with time zone", typeOf(TIMESTAMP_WITH_TIME_ZONE()));
  EXPECT_EQ("date", typeOf(DATE()));

  EXPECT_EQ("bingtile", typeOf(BINGTILE()));

  EXPECT_EQ("json", typeOf(JSON()));

  EXPECT_EQ("HyperLogLog", typeOf(HYPERLOGLOG()));

  EXPECT_EQ("tdigest(double)", typeOf(TDIGEST(DOUBLE())));

  EXPECT_EQ("unknown", typeOf(UNKNOWN()));

  EXPECT_EQ("array(integer)", typeOf(ARRAY(INTEGER())));
  EXPECT_EQ("map(varchar, array(date))", typeOf(MAP(VARCHAR(), ARRAY(DATE()))));
  EXPECT_EQ(
      "row(integer, varchar, array(date))",
      typeOf(ROW({INTEGER(), VARCHAR(), ARRAY(DATE())})));
  EXPECT_EQ(
      "row(\"a\" integer, \"b\" varchar, \"c\" array(date))",
      typeOf(ROW({"a", "b", "c"}, {INTEGER(), VARCHAR(), ARRAY(DATE())})));

  POLLUX_ASSERT_THROW(typeOf(OPAQUE<int>()), "Unsupported type: OPAQUE<int>");
}

} // namespace
} // namespace kumo::pollux::functions
